package com.xxg;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xxg.curd.Curd;
import com.xxg.curd.CurdConfig;
import com.xxg.curd.ex.CurdException;
import com.xxg.curd.impl.MasterCurd;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequestMapping("/curd")
@RestController
public class CurdController {

    private final Curd curd;

    @Autowired
    public CurdController(CurdConfig config) {
        this.curd = new MasterCurd(config);
    }

    @PostMapping("/{tableName}")
    public Object insert(@PathVariable String tableName, @RequestBody String request){
        Object req = JSON.parse(request);
        JSONArray arr;
        if(req instanceof JSONObject){
            arr = new JSONArray();
            arr.add(req);
        }else {
            arr = (JSONArray) req;
        }
        return curd.add(tableName, (List)arr);
    }

    @DeleteMapping("/{tableName}")
    public Object delete(@PathVariable String tableName, @RequestBody String request){
        Object req = JSON.parse(request);
        JSONArray arr = new JSONArray();
        if(req instanceof Long || req instanceof String){
            arr.add(req);
        }else if(req instanceof JSONArray){
            arr = (JSONArray) req;
        }else {
            throw new CurdException("参数不正确");
        }

        List<String> ids = arr.stream().map(e -> e.toString()).collect(Collectors.toList());
        return curd.deleteById(tableName, ids);

    }

    @PutMapping("/{tableName}")
    public Object update(@PathVariable String tableName, @RequestBody String request){
        Object req = JSON.parse(request);
        JSONArray arr;
        if(req instanceof JSONObject){
            arr = new JSONArray();
            arr.add(req);
        }else {
            arr = (JSONArray) req;
        }
        return curd.updateById(tableName, (List)arr);

    }

    @PostMapping("/select")
    public Object select(@RequestBody String request){
        JSONObject obj = JSON.parseObject(request);
        Object skip = obj.remove("skip");
        Object size = obj.remove("size");

        if(null == skip || size == null){
            if (obj.size() == 1) {
                String tableName = obj.keySet().stream().findFirst().get();
                Object data = obj.get(tableName);
                List<String> ids;
                if(data instanceof String){
                    ids = Arrays.asList((String)data);
                }else if(data instanceof Integer){
                    ids = Arrays.asList(data.toString());
                }else if(data instanceof Long){
                    ids = Arrays.asList(data.toString());
                }else if(data instanceof JSONArray){
                    ids = ((JSONArray) data).stream().map(e -> e.toString()).collect(Collectors.toList());
                }else {
                    throw new CurdException("参数不正确");
                }

                return curd.findById(tableName,ids);
            }else {
                throw new CurdException("参数不正确");
            }
        }

        if(obj.size() == 1){
            String tableName = obj.keySet().stream().findFirst().get();
            JSONObject data = obj.getJSONObject(tableName);
            return curd.pageBy(tableName,(Integer) skip,(Integer) size,(Map)data);
        }else {
            throw new CurdException("参数不正确");
        }
    }
}
